<!-- 商品列表页面 -->
<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card区域 -->
    <el-card>
      <!-- 查询和添加商品区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button
              icon="el-icon-search"
              slot="append"
              @click="getGoodsList"
            >
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button
            type="primary"
            @click="addGoods"
          >添加商品</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="goodsList"
        style="width: 100%"
        stripe
        border
      >
        <el-table-column
          type="index"
          label="#"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="600px"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="价格（元）"
        >
        </el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品数量"
        >
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
        >
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="150"
        >
          <template slot-scope="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="130px"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editUser(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteUser(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

  </div>
</template>

<script>
export default {
  name: "Goods",
  data() {
    return {
      goodsList: [],
      queryInfo: {
        // 查询参数
        query: "",
        // 页码
        pagenum: 1,
        // 每页的数据量
        pagesize: 5,
      },
      total: 0,
      goodsList: [],
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const res = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.data.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      this.goodsList = res.data.data.goods;
      this.total = res.data.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage;
      this.getGoodsList();
    },
    editUser(item) {
      console.log(item);
    },
    async deleteUser(id) {
      const { data: res } = await this.$http.delete("goods/" + id);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.getGoodsList();
      this.$message.success(res.meta.msg);
    },
    addGoods() {
      this.$router.push("/add");
    },
  },
};
</script>

<style  scoped>
</style>
